﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using QuanLiNhaSachDTO;

namespace QuanLiNhaSachDAO
{
    public class NhaCungCapDAO
    {
        public static DataTable selectAll()
        {
            string sql = "SELECT * FROM NHACUNGCAP";
            SqlDataAdapter da = new SqlDataAdapter(sql, sqlDB.MyConnection);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }

        public static void insert(NhaCungCap m)
        {
            string sql = "INSERT INTO NHACUNGCAP(Ten,Email,DienThoai,DiaChi,GhiChu) VALUES(@Ten,@Email,@DienThoai,@DiaChi,@GhiChu)";
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = sql;
            cmd.Connection = sqlDB.MyConnection;
            cmd.Parameters.AddWithValue("@Ten", m.Ten);
            cmd.Parameters.AddWithValue("@Email", m.Email);
            cmd.Parameters.AddWithValue("@DienThoai", m.DienThoai);
            cmd.Parameters.AddWithValue("@DiaChi", m.DiaChi);
            cmd.Parameters.AddWithValue("@GhiChu", m.GhiChu);
            cmd.ExecuteNonQuery();
        }

        public static void update(NhaCungCap m)
        {
            string sql = "UPDATE NHACUNGCAP SET Ten = @Ten, Email = @Email, DienThoai = @DienThoai, DiaChi = @DiaChi, GhiChu = @GhiChu WHERE id = @id";
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = sql;
            cmd.Connection = sqlDB.MyConnection;
            cmd.Parameters.AddWithValue("@id", m.Id);
            cmd.Parameters.AddWithValue("@Ten", m.Ten);
            cmd.Parameters.AddWithValue("@Email", m.Email);
            cmd.Parameters.AddWithValue("@DienThoai", m.DienThoai);
            cmd.Parameters.AddWithValue("@DiaChi", m.DiaChi);
            cmd.Parameters.AddWithValue("@GhiChu", m.GhiChu);
            cmd.ExecuteNonQuery();
        }

        public static void delete(NhaCungCap q)
        {
            string sql = "DELETE FROM NHACUNGCAP WHERE id = @id";
            SqlCommand cmd = new SqlCommand(sql, sqlDB.MyConnection);
            cmd.Parameters.AddWithValue("@id", q.Id);
            cmd.ExecuteNonQuery();
        }

        public static DataTable search(string id)
        {
            string sql = "SELECT * FROM NHACUNGCAP WHERE id ='" + id + "'";
            SqlDataAdapter da = new SqlDataAdapter(sql, sqlDB.MyConnection);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }

        public static DataTable TimSachTuNhaCungCap(string id)
        {
            string sql = "SELECT * FROM SACH WHERE MaNhaCungCap = '" + id + "'";
            SqlDataAdapter da = new SqlDataAdapter(sql, sqlDB.MyConnection);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }
    }
}
